package com.example.travel.controller;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.travel.common.AjaxResult;
import com.example.travel.common.TableDataInfo;
import com.example.travel.domain.entity.Article;
import com.example.travel.domain.request.ArticleNameRequest;
import com.example.travel.domain.request.PageQuery;
import com.example.travel.interceptor.TokenUtils;
import com.example.travel.mapper.SpotMapper;
import com.example.travel.service.ArticleService;
import io.swagger.annotations.Api;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author : LinQingLiang
 * @file : ArticleController.java
 * @Since : 2023/2/14 21:37
 */

@RestController
@RequestMapping("/article")
@Api(tags = "评论")
public class ArticleController extends BaseController{

    @Autowired
    private ArticleService articleService;

    @Autowired
    private SpotMapper spotMapper;

    @GetMapping("list")
    public TableDataInfo list(@Validated PageQuery pageQuery, @Param("name") String name){
        Page page1 = articleService.page(pageQuery.getPageNum(),pageQuery.getPageSize(),name);
        return getDataTable(page1.getRecords(),page1.getTotal());
    }

    @PostMapping("/insertArticle")
    public AjaxResult insertArticle(@RequestBody ArticleNameRequest nameRequest){
        //相当于new data
        Article article  = new Article();
        article.setTime(DateUtil.now());
        article.setUser(TokenUtils.getUser().getNickName());
        article.setContent(nameRequest.getContent());
        article.setName(nameRequest.getName());
        articleService.save(article);
        int row = spotMapper.updateArticle(article.getId(),nameRequest.getSpotName());
        if(row > 0 ){
            return AjaxResult.success("发表文章成功");
        }
        return AjaxResult.error("发表文章失败");
    }


    @PutMapping("/updateArticle")
    public AjaxResult updateArticle(@RequestBody ArticleNameRequest nameRequest){
        spotMapper.updateArticle(nameRequest.getId(),nameRequest.getSpotName());
        Article article = new Article();
        BeanUtils.copyProperties(nameRequest,article);
        return AjaxResult.success(articleService.updateById(article));
    }

    @GetMapping("/{id}")
    public AjaxResult selectById(@PathVariable Integer id){
        Article article = articleService.getById(id);

        ArticleNameRequest nameRequest = new ArticleNameRequest();
        BeanUtils.copyProperties(article,nameRequest);
        String spotName = spotMapper.selectByArticleId(id);
        nameRequest.setSpotName(spotName);
        return AjaxResult.success(nameRequest);
    }

    //删除个人
    @DeleteMapping("deleteArticle/{id}")
    public AjaxResult deleteUser(@PathVariable Integer id){
        LambdaQueryWrapper<Article> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(Article::getId,id);
        return AjaxResult.success(articleService.remove(lambdaQueryWrapper));
    }

    //批量删除
    @DeleteMapping("deleteArticles/{ids}")
    public AjaxResult deleteUser(@PathVariable List<Integer> ids){
        return AjaxResult.success(articleService.removeBatchByIds(ids));
    }


}
